$(function(){
	var ctx = $("#ctx").val();
	var t = $("#mytree").mytree({
		callback : {
			onAsyncSuccess: function(){
				t.expandAll(true);
			},
			onClick : select
		},
		async: {
			enable: true,
			url: ctx + "/admin/channel/channelTreeAll"
		}
	});
	$("#channel").click(showMenu);

	$("#publishDate").datepicker({
		dateFormat : "yy-mm-dd",
		maxDate : 0,
		changeMonth : true,
		changeYear : true
	});
	$("#ui-datepicker-div").css("font-size", "85%");
	var editor = $("#content").xheditor({
		tools:'full'
	});
	$("#keyword").keywordinput();
	$("#keyword").autocomplete({
		source : ctx + "/admin/topic/findKeyword",
		minLength : 2
	});

	$("#attach").uploadify({
		swf : ctx + "/resources/uploadify/uploadify.swf",
		uploader : (ctx + "/admin/topic/upload"),
		fileObjName : "attach",
		auto : false,
		fileSizeLimit : "90000KB",
		fileTypeExts : "*.jpg;*.png;*.flv;*.swf;*.mp4;*.pdf;*.doc;*.xls;*.ppt;*.rar;*.zip;",
		formData : {
			"sid" : $("#sid").val()
		},
		onUploadSuccess : function(file, data, response){
			var ao = $.parseJSON(data);
			var suc = $.ajaxCheck(ao);
			if(suc){
				$("#attachmentTable").find("tbody").append(createAttacmentRow(ao.obj));
			}
		}
	});
	$("#upload").click(function(){
		$("#attach").uploadify("upload", "*");
	});
	$("#attachmentTable").on("click", ".isIndexPic", function(){
		dwrService.updateIsIndexPic($(this).val());
	});
	$("#attachmentTable").on("click", ".isAttachment", function(){
		dwrService.updateIsAttachment($(this).val());
	});
	var filePath = ctx + "/resources/upload/";
	$("#attachmentTable").on("click", ".insertAttachment", function(){
		var fileType = $(this).attr("fileType");
		var id = $(this).attr("title");
		var storeName = $(this).attr("name");
		var orgName = $(this).attr("orgname");
		var node = "";
		//判断是不是图片
		if("IMAGE"==fileType){
			node += "<img src='"+ filePath + storeName +"' id='attach_" + id + "'/>";
		}else{
			node += "<a href='"+ filePath + storeName +"' id='attach_" + id + "'>" + orgName + "</a>";
		}
		editor.pasteHTML(node);
	});
	//删除附件需事件委派
	$("#attachmentTable").on("click", ".delete", function(event){
		var that = this;
		var aid = $(this).attr("title");
		var ensure = confirm("该操作不可逆,确定进行吗?");
		if(ensure){
			dwrService.deleteAttachment($(this).attr("title"), function(){
				$(that).parent("td").parent("tr").remove();
				//删除editor文章内容里的附件链接
				$("#xhe0_iframe").contents().find("#attach_" + aid).remove();
			});
		}
		event.preventDefault();
	});

	function select(event, treeId, treeNode){
		if(undefined==treeNode || treeNode.isParent){
			return;
		}
		$("#channel").val(treeNode.name);
		$("#channelId").val(treeNode.id);
		hideMenu();
	}
	function showMenu() {
		$("#mytree").width($(this).width()-9);
		var cObj = $("#channel");
		var cOffset = $("#channel").offset();
		$("#menuContent").css({left:cOffset.left + "px", top:cOffset.top + cObj.outerHeight() + "px"}).slideDown("fast");
		$("body").bind("mousedown", onBodyDown);
	}
	function onBodyDown(event) {
		if (!(event.target.id == "menuBtn" || event.target.id == "menuContent" || $(event.target).parents("#menuContent").length>0)) {
			hideMenu();
		}
	}
	function hideMenu() {
		$("#menuContent").fadeOut("fast");
		$("body").unbind("mousedown", onBodyDown);
	}

	$("#addBtn").click(function(){
		$("#addForm").submit();
		$(this).attr("disabled");
	});
	$("#resetBtn").click(function(){
		$("#addForm")[0].reset();
	});
	$("#closeBtn").click(function(){
		window.close();
	});

	function createAttacmentRow(a){
		var rowStr = "<tr>";
		if("IMAGE" == a.fileType){
			rowStr += "<td><img src='" + $("#ctx").val() + "/resources/upload/thumb/" + a.storeName + "'/></td>";
		}else{
			rowStr += "<td>无缩略图</td>";
		}
		rowStr += "<td>" + a.originalName + "</td>";
		rowStr += "<td>" + Math.round(a.size/1024) + "KB</td>";
		if("IMAGE" == a.fileType){
			rowStr += "<td><input type='checkbox' class='isIndexPic' value='" + a.id + "' name='isIndexPic'></td>";
			rowStr += "<td><input type='radio' class='channelPicId' value='" + a.id + "' name='channelPicId'></td>";
		}else{
			rowStr += "<td>&nbsp</td><td>&nbsp</td>";
		}
		rowStr += "<td><input type='checkbox' class='isAttachment' value='" + a.id + "' name='isAttachment'></td>";
		rowStr += "<td><a href='#' class='insertAttachment list_op' title='" + a.id + "' fileType='" + a.fileType + "' name='" + a.storeName + "' orgname='" + a.originalName + "'>插入附件</a>"
			+"&nbsp&nbsp<a href='#' title='" + a.id + "' class='delete list_op'>删除附件</a></td>";
		rowStr += "<input type='hidden' name='attachmentId' value='" + a.id + "'/>"
		rowStr += "</tr>";
		return rowStr;
	}

});